<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - AI对话系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .auth-container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            width: 400px;
        }

        .auth-title {
            text-align: center;
            margin-bottom: 25px;
            color: #2c3e50;
        }

        .auth-form .form-group {
            margin-bottom: 20px;
        }

        .auth-form label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }

        .auth-form input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        .auth-form button {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .auth-form button:hover {
            background-color: #2980b9;
        }

        .error-message {
            color: #e74c3c;
            margin-bottom: 15px;
            text-align: center;
            display: none;
        }

        .auth-switch {
            text-align: center;
            margin-top: 20px;
            color: #7f8c8d;
        }

        .auth-switch a {
            color: #3498db;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <h2 class="auth-title" id="auth-title">登录</h2>
        <div id="error-message" class="error-message"></div>

        <form id="auth-form" class="auth-form">
            <div id="username-group" class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>

            <div id="email-group" class="form-group" style="display: none;">
                <label for="email">电子邮箱</label>
                <input type="email" id="email" name="email">
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>

            <button type="submit" id="auth-submit">登录</button>
        </form>

        <div class="auth-switch">
            <span id="auth-switch-text">没有账号？</span>
            <a id="auth-switch-btn">注册</a>
        </div>
    </div>

    <script>
        // 切换登录/注册模式
        document.getElementById('auth-switch-btn').addEventListener('click', function() {
            const isLogin = document.getElementById('auth-title').textContent === '登录';

            if (isLogin) {
                document.getElementById('auth-title').textContent = '注册';
                document.getElementById('auth-submit').textContent = '注册';
                document.getElementById('auth-switch-text').textContent = '已有账号？';
                document.getElementById('auth-switch-btn').textContent = '登录';
                document.getElementById('email-group').style.display = 'block';
            } else {
                document.getElementById('auth-title').textContent = '登录';
                document.getElementById('auth-submit').textContent = '登录';
                document.getElementById('auth-switch-text').textContent = '没有账号？';
                document.getElementById('auth-switch-btn').textContent = '注册';
                document.getElementById('email-group').style.display = 'none';
            }

            // 清空错误消息
            document.getElementById('error-message').style.display = 'none';
        });

        // 处理表单提交
        document.getElementById('auth-form').addEventListener('submit', async function(e) {
            e.preventDefault();

            const isLogin = document.getElementById('auth-title').textContent === '登录';
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const email = isLogin ? null : document.getElementById('email').value;
            const errorElement = document.getElementById('error-message');

            const endpoint = isLogin ? '/api/login' : '/api/register';
            const data = isLogin ? { username, password } : { username, email, password };

            try {
                const response = await fetch(endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                if (result.success && result.user) {
                // 从返回的 user 对象中提取 id
                    const userId = result.user.id;
                    localStorage.setItem('user_id', userId);

                  console.log('登录成功，user_id 已保存:', userId);
                  // 跳转到用户页面或执行其他操作
                }
                if (response.ok) {
                    if (isLogin) {
                        // 登录成功，跳转到主页面
                        window.location.href = '/';
                    } else {
                        // 注册成功，切换回登录界面
                        document.getElementById('auth-title').textContent = '登录';
                        document.getElementById('auth-submit').textContent = '登录';
                        document.getElementById('auth-switch-text').textContent = '没有账号？';
                        document.getElementById('auth-switch-btn').textContent = '注册';
                        document.getElementById('email-group').style.display = 'none';

                        errorElement.textContent = '注册成功，请登录';
                        errorElement.style.color = '#2ecc71';
                        errorElement.style.display = 'block';
                    }
                } else {
                    // 显示错误消息
                    errorElement.textContent = result.error || (isLogin ? '登录失败' : '注册失败');
                    errorElement.style.color = '#e74c3c';
                    errorElement.style.display = 'block';
                }
            } catch (error) {
                console.error('认证错误:', error);
                errorElement.textContent = '网络错误，请重试';
                errorElement.style.color = '#e74c3c';
                errorElement.style.display = 'block';
            }
        });


    </script>
</body>
</html>
